package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;

    //菜品分页查询
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //查询数据
        List page = dishMapper.page(dishPageQueryDTO);
        //封装返回
        Page pageResult = (Page) page;
        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }
    //菜品新增
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        //补全菜品信息,新增进菜品表dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.save(dish);
        //新增其口味到口味表dish_flavor
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long dishId = dish.getId();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }
    //菜品批量删除
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //批量删除菜品
        dishMapper.delete(ids);
        //批量删除菜品口味
        dishFlavorMapper.deleteByDishId(ids);
    }
    //根据Id查询菜品
    @Override
    public DishVO getById(Long id) {
        //查询菜品dish数据
        Dish dish = dishMapper.getById(id);
        //  String categoryName = categoryMapper
        //查询菜品口味dish_flavor数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        //封装dishVo
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }
    //修改菜品
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        //更新菜品表dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //修改dish_flavor表
        //先删除旧dish_flavor数据
        dishFlavorMapper.deleteByDishId(Arrays.asList(dishDTO.getId()));
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long dishId = dish.getId();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }
    //根据分类Id查询菜品
    @Override
    public List listBycategoryId(Integer categoryId) {
        Dish dish = new Dish();
        DishVO dishVO = new DishVO();
        return dishMapper.listBycategoryId(categoryId);
    }
    //菜品起售停售
    @Override
    public void permission(Integer status, Long id) {
        dishMapper.permission(status,id);
    }
}
